<template>
  <div id="topSearch">
    <div class="search-box">
      <el-form
        :inline="true"
        :model="searchInfo"
        ref="ruleForm"
        class="demo-form-inline"
      >
        <el-form-item label="商品名称" prop="cardName">
          <el-input
            size="small"
            v-model="searchInfo.cardName"
            placeholder="请您输入商品名称"
            maxlength="100"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="商品类型" prop="type">
          <el-select
            size="small"
            v-model="searchInfo.type"
            placeholder="全部"
            clearable
          >
            <el-option
              v-for="(item, index) in goodsTypeList"
              :key="index"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            size="small"
            v-model="searchInfo.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品状态" prop="status">
          <el-select
            size="small"
            v-model="searchInfo.status"
            placeholder="全部"
            clearable
          >
            <el-option
              v-for="(item, index) in goodStatusList"
              :key="index"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div>
          <el-form-item label="库存" prop="stock">
            <el-select
              size="small"
              v-model="searchInfo.stock"
              placeholder="全部"
              clearable
            >
              <el-option
                v-for="(item, index) in stockList"
                :key="index"
                :label="item.dictLabel"
                :value="item.dictValue"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="s-btn">
            <el-button type="success" icon="el-icon-search" @click="toSearch">
              查询
            </el-button>
            <el-button type="success" plain @click="resetForm('ruleForm')">
              重置
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  data() {
    return {
      searchInfo: {
        cardName: '', //商品名称
        type: '', //商品类型
        startTime: '', //开始时间
        endTime: '', //结束时间
        status: '', //商品状态
        stock: '', // 库存
        createTime: [] //创建时间
      },
      goodsTypeList: [{ dictLabel: '全部', dictValue: '' }],
      goodStatusList: [{ dictLabel: '全部', dictValue: '' }],
      stockList: [
        { dictLabel: '全部', dictValue: '' },
        { dictLabel: '0', dictValue: 0 }
      ]
    }
  },
  watch: {
    'searchInfo.createTime': {
      handler(val) {
        this.searchInfo.startTime = val && val.length ? val[0] : ''
        this.searchInfo.endTime = val && val.length ? val[1] : ''
      },
      deep: true
    }
  },
  async mounted() {
    const { goodsTypeList, goodStatusList } = this
    this.goodsTypeList = goodsTypeList.concat(
      await sysNormalDisable('Integral_commodity_type')
    )
    this.goodStatusList = goodStatusList.concat(
      await sysNormalDisable('Integral_commodity_status')
    )
  },
  methods: {
    toSearch() {
      const obj = Object.assign({}, this.searchInfo)
      delete obj.createTime
      this.$emit('toSearch', obj)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="less" scoped></style>
